package com.example.demo.domain;

import com.example.demo.enums.ClauseCombine;
import com.example.demo.service.Clause;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Data
@EqualsAndHashCode(callSuper = true)
public class CombineClause extends ArrayList<Clause> implements Clause {

    private ClauseCombine combine;

    public CombineClause(ClauseCombine combine) {
        this.combine = combine;
    }

    public CombineClause(ClauseCombine combine, Collection<Clause> clauses) {
        super(clauses.stream().filter(Objects::nonNull).collect(Collectors.toList()));
        this.combine = combine;
    }
    // 添加一个或多个Clause到当前列表中，并返回当前对象（支持链式调用）
    public CombineClause join(Clause... clauses) {
        if (clauses == null) {
            return this;
        }
        for (Clause clause : clauses) {
            if (clause != null) {
                this.add(clause);
            }
        }
        return this;
    }
    // 静态工厂方法，创建一个AND组合的CombineClause
    public static CombineClause and(Clause... clauses) {
        List<Clause> list = Arrays.stream(clauses).filter(Objects::nonNull).collect(Collectors.toList());
        return new CombineClause(ClauseCombine.And, list);
    }

    public static CombineClause or(Clause... clauses) {
        List<Clause> list = Arrays.stream(clauses).filter(Objects::nonNull).collect(Collectors.toList());
        return new CombineClause(ClauseCombine.Or, list);
    }

}
